package structural.proxy.dynamic_proxy.cglib;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName: MyMthod
 * @Description: 自定义方法拦截器
 * @Author: liu-hao
 * @Date: 2023-04-10 19:38
 * @Version: 1.0
 **/
public class MyMethodInterceptor implements MethodInterceptor {

    private Object target;

    public MyMethodInterceptor(Object target) {
        this.target = target;
    }

    private void before() {
        System.out.println("处理请求之前");
    }

    private void after() {
        System.out.println("处理请求之后");
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object result = method.invoke(this.target, objects);
        after();
        return result;
    }
}
